package pl.vanta.mjuzik;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

import pl.vanta.mjuzik.gui.WindowHandler;
import pl.vanta.mjuzik.gui.actions.OptionsAction;
import pl.vanta.mjuzik.gui.panels.DetailsPanel;
import pl.vanta.mjuzik.gui.panels.PlayerControllerPanel;
import pl.vanta.mjuzik.gui.panels.PlaylistButtonsPanel;
import pl.vanta.mjuzik.gui.panels.PlaylistPanel;
import pl.vanta.mjuzik.guice.modules.MjuzikModule;
import pl.vanta.mjuzik.services.ActionService;
import pl.vanta.mjuzik.services.ConfigurationService;
import pl.vanta.utils.gui.actions.ExitAction;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class Starter
{
	public static void main(String[] args)
	{
		Injector injector = Guice.createInjector(new MjuzikModule());

		ConfigurationService configurationService = injector.getInstance(ConfigurationService.class);
		ApplicationController controller = injector.getInstance(ApplicationController.class);
		ActionService actionService = injector.getInstance(ActionService.class);

		PlayerControllerPanel playerControllerPanel = new PlayerControllerPanel();
		PlaylistPanel playlistPanel = new PlaylistPanel(controller, configurationService);
		PlaylistButtonsPanel buttonsPanel = new PlaylistButtonsPanel(actionService);
		DetailsPanel detailsPanel = new DetailsPanel(configurationService);

		playerControllerPanel.addListener(controller);
		playlistPanel.addListener(controller);
		buttonsPanel.addListener(controller);

		controller.setTable(playlistPanel.getTable());
		controller.setControllerPanel(playerControllerPanel);

		controller.addPlayerProgressListener(detailsPanel);
		controller.addPlayerProgressListener(playerControllerPanel);
		controller.addPlayerLoadedListener(detailsPanel);
		controller.addPlayerLoadedListener(playerControllerPanel);

		controller.init();

		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());
		mainPanel.add(playerControllerPanel, BorderLayout.SOUTH);
		mainPanel.add(playlistPanel, BorderLayout.CENTER);
		mainPanel.add(buttonsPanel, BorderLayout.EAST);
		mainPanel.add(detailsPanel, BorderLayout.NORTH);

		JFrame frame = createFrame(mainPanel, configurationService);

		createActions(frame, configurationService, actionService);

		frame.setJMenuBar(createMenuBar(actionService));
		frame.addWindowListener(new WindowHandler(configurationService));

		frame.setVisible(true);
	}

	private static JMenuBar createMenuBar(ActionService actionService)
	{
		JMenu menuFile = new JMenu("File");
		menuFile.add(actionService.getAction(ApplicationConstants.ACTION_OPTIONS));
		menuFile.addSeparator();
		menuFile.add(actionService.getAction(ApplicationConstants.ACTION_EXIT));

		JMenu menuPlaylist = new JMenu("Playlist");

		JMenuBar menuBar = new JMenuBar();

		menuBar.add(menuFile);
		menuBar.add(menuPlaylist);

		return menuBar;
	}

	private static JFrame createFrame(JPanel panel, ConfigurationService configuration)
	{
		JFrame frame = new JFrame();
		frame.getContentPane().setLayout(new BorderLayout());
		frame.getContentPane().add(panel);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setLocationRelativeTo(null);

		frame.setTitle(String.format("%s %s", configuration.getApplicationName(), configuration.getApplicationVersion()));

		if (configuration.getOptions().getWindowBounds() != null)
		{
			frame.setBounds(configuration.getOptions().getWindowBounds());
		}

		return frame;
	}

	private static void createActions(JFrame frame, ConfigurationService configurationService, ActionService actionService)
	{
		actionService.registerAction(ApplicationConstants.ACTION_OPTIONS, new OptionsAction(frame, configurationService));
		actionService.registerAction(ApplicationConstants.ACTION_EXIT, new ExitAction());
	}

	private static void createTray()
	{
		/*
		 * if (!SystemTray.isSupported()) {
		 * System.out.println("SystemTray is not supported"); return; }
		 * 
		 * MenuItem aboutItem = new MenuItem("About"); CheckboxMenuItem cb1 =
		 * new CheckboxMenuItem("Set auto size"); CheckboxMenuItem cb2 = new
		 * CheckboxMenuItem("Set tooltip"); Menu displayMenu = new
		 * Menu("Display"); MenuItem errorItem = new MenuItem("Error"); MenuItem
		 * warningItem = new MenuItem("Warning"); MenuItem infoItem = new
		 * MenuItem("Info"); MenuItem noneItem = new MenuItem("None"); MenuItem
		 * exitItem = new MenuItem("Exit");
		 * 
		 * final PopupMenu popup = new PopupMenu();
		 * 
		 * popup.add(aboutItem); popup.addSeparator(); popup.add(cb1);
		 * popup.add(cb2); popup.addSeparator(); popup.add(displayMenu);
		 * displayMenu.add(errorItem); displayMenu.add(warningItem);
		 * displayMenu.add(infoItem); displayMenu.add(noneItem);
		 * popup.add(exitItem);
		 * 
		 * final TrayIcon trayIcon = new TrayIcon(createImage("images/bulb.gif",
		 * "tray icon", popup));
		 * 
		 * final SystemTray tray = SystemTray.getSystemTray();
		 * 
		 * try { tray.add(trayIcon); } catch (AWTException e) {
		 * System.out.println("TrayIcon could not be added."); }
		 */
	}
}